package com.leapp.sk.wxapi;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.mvp.activity.BaseActivityMVP;
import com.example.mvp.binder.BaseDataBinder;
import com.leapp.sk.bean.WXRespEntity;
import com.leapp.sk.bean.WXTokenEntity;
import com.leapp.sk.binder.login.WXEntryLoginBinder;
import com.leapp.sk.util.wxentry.WXLoginUtils;
import com.leapp.sk.viewdelegate.login.WXEntryLoginDelegate;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import java.util.HashMap;
import java.util.Map;

import cc.vv.lkbasecomponent.http.LKHttp;
import cc.vv.lkbasecomponent.http.lib.CallBack;
import cc.vv.lkbasecomponent.http.lib.setting.Settings;
import cc.vv.lkbasecomponent.util.LKJsonUtil;
import cc.vv.lklibrary.log.LogOperate;

/**
 * @author chaohaipeng  on 2018/7/9.
 */

public class WXMEntryActivity extends BaseActivityMVP<WXEntryLoginDelegate> implements IWXAPIEventHandler {
    @Override
    protected void initDate() {
        IWXAPI api = WXLoginUtils.initWXEntryLogin(this);
        WXLoginUtils.checkIntent(this,api);
    }

    @Override
    protected void bindEvenListener() {

    }

    @Override
    protected Class<WXEntryLoginDelegate> getDelegateClass() {
        return WXEntryLoginDelegate.class;
    }

    @Override
    public BaseDataBinder getDataBinder() {
        return new WXEntryLoginBinder();
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {

    }

    @Override
    public void onReq(BaseReq baseReq) {
        LogOperate.e("微信分析==="+baseReq.toString());
        WXRespEntity entity = LKJsonUtil.parseJsonToBean(baseReq.toString(), WXRespEntity.class);
        Map<String ,Object> mutableMapOf=new HashMap<>();
        mutableMapOf.put("appid","wxb363a9ff53731258");
        mutableMapOf.put("secret","2b0d0325bb7c8383bff52e0900b7f56c");
        mutableMapOf.put("code",entity.getCode());
        mutableMapOf.put("grant_type","authorization_code");
        LKHttp.get("https://api.weixin.qq.com/sns/oauth2/access_token",mutableMapOf,WXTokenEntity.class, new  CallBack<WXTokenEntity>(){
            @Override
            public void onSuccess(String url, WXTokenEntity obj) {
                super.onSuccess(url, obj);
            }

            @Override
            public void onFailure(String url, boolean isLoading, String exceptionStr) {
                super.onFailure(url, isLoading, exceptionStr);
            }
        } ,false,new Settings(5000));
    }

    @Override
    public void onResp(BaseResp baseResp) {
        LogOperate.e("微信分析==="+baseResp.toString());
    }
}
